
const data0 = JSON.parse(localStorage.getItem("data"));
const data = data0.data;

// 页面渲染
function renderBody(data, currentPage = 1) {
    singlePageNum = 5;
    renderPaging(data, currentPage);
    const statIndex = singlePageNum * (currentPage - 1);
    const endIndex = statIndex + singlePageNum;
    const newData = data.slice(statIndex, endIndex);
    $("#information tbody").empty();
    for (let i = 0; i < newData.length; ++i) {
        const checked = data[i].isComplete ? "checked" : "";
        $("tbody").append(`<tr> <td><input type="checkbox" ${checked}> ${newData[i].name}</td><td>${newData[i].id}</td><td>${newData[i].phone}</td><td>${newData[i].department}</td><td>${newData[i].position}</td><td>${newData[i].type}</td><td>${newData[i].fixedSalary}</td><td>${newData[i].meritPay}</td><td>${newData[i].totalWages}</td><td>${newData[i].employmentDates}</td> <td>${newData[i].PositiveDates}</td>
            <td>
            <button type="button" class="btn btn-info particulars" id="${newData[i].id}">定薪</button>
            <button type="button" class="btn btn-info btn-lg delete" data-toggle="modal" data-target="#myModal"
            id="${newData[i].id}" >删除</button>
            </td>
        </tr>
            `
        );
    }
    List(data);

}
// 搜索条件的下拉菜单
function List(data) {
    $("#departmentList").empty();
    $("#positionList").empty();
    let positionList = ["全部"];
    let departmentList = ["全部"];
    for (let i = 0; i < data.length; ++i) {
        if (positionList.indexOf(data[i].position) === -1) {
            positionList.push(data[i].position);
        }
        if (departmentList.indexOf(data[i].department) === -1) {
            departmentList.push(data[i].department);
        }
    }
    for (let i = 0; i < positionList.length; ++i) {
        $("#positionList").append(` <option value="${positionList[i]}">${positionList[i]}</option>`);
    }
    for (let i = 0; i < departmentList.length; ++i) {
        $("#departmentList").append(` <option value = ${departmentList[i]}>${departmentList[i]}</option>`);
    }
}

// 定义搜索条件
let seek = [];
// 显示搜索条件
$(".condition").on("change", "input", function () {
    let text = $(this).val();
    const parentId = $(this).parent()[0].id;
    seek.push(text);

    // console.log(seek);
});

// 计算显示页码
function renderPaging(data, currentPage = 1) {
    $("#pages").empty();
    const num = Math.ceil(data.length / singlePageNum);
    for (let i = 1; i <= num; ++i) {
        $("#pages").append(`<span>${i}</span>`);
    }
    $("#pages > span").eq(currentPage - 1).addClass("currentPage");
}

// 重置
$("#reset").on("click", function () {
    $("#screen").empty();
    $("#positionList").val("全部");
    $("#departmentList").val("全部");
    $("#screen").append(`<span class="screen">筛选条件：</span>`);
    $("#name input").val("");
    $("#name phone").val("");
    seek = [];

    renderBody(data, currentPage = 1);
});

// 搜索
$("#search").on("click", function () {
    let position = $("#positionList").val();
    let department = $("#departmentList").val();
    for (let key in seek) {
        $("#screen").append(` <span class="trem" id=${key}>${seek[key]}</span> `);
    }
    $("#screen").append(` 
    <span class="trem" id=${position}>${position}</span>
    <span class="trem" id=${department}>${department}</span>
     `);

    let newData = [];
    let newData1 = [];
    let newData2 = [];
    if (position !== "全部" || department !== "全部") {
        if (position !== "全部" && department === "全部") {
            for (let i = 0; i < data.length; ++i) {
                if (data[i].position === position) {
                    newData.push(data[i]);
                }
            }
        }
        if (department !== "全部" && position === "全部") {
            for (let i = 0; i < data.length; ++i) {
                if (data[i].department === department) {
                    newData.push(data[i]);
                }
            }
        }
        
    }
    else {
        newData = data;
    }


    if (seek.length < 2) {
        if (seek.length < 1) {
            renderBody(newData, currentPage = 1);
            $("#pages").on("click", "span", function (event) {
                const currentPage = parseInt($(event.target).text());
                renderBody(newData, currentPage);
            });
        }
        else {
            for (let i = 0; i < seek.length; ++i) {
                for (let j = 0; j < newData.length; ++j) {
                    // console.log(newData.indexOf(newData[j]))
                    for (let key in newData[j]) {
                        if (seek[i] === newData[j][key]) {
                            newData1.push(newData[j]);
                        }
                    }
                }
            }
            renderBody(newData1, currentPage = 1);
            $("#pages").on("click", "span", function (event) {
                const currentPage = parseInt($(event.target).text());
                renderBody(newData1, currentPage);
            });
        }
    } else {
        for (let i = 0; i < seek.length; ++i) {
            for (let j = 0; j < newData.length; ++j) {
                // console.log(newData.indexOf(newData[j]))
                for (let key in newData[j]) {
                    if (seek[i] === newData[j][key]) {
                        newData1.push(newData[j]);
                    }
                }
            }
        }
        console.log(newData1);
        for (let j = 0; j < newData1.length; ++j) {
            // console.log(newData.indexOf(newData[j]))
            for (let key in newData1[j]) {
                console.log(typeof newData1[j][key]);
                if (Number(seek[1]) === newData1[j][key]) {
                    newData2.push(newData1[j]);
                }
            }
        }
        renderBody(newData2, currentPage = 1);
        $("#pages").on("click", "span", function (event) {
            const currentPage = parseInt($(event.target).text());
            renderBody(newData2, currentPage);
        });
    }
    seek = [];
});



// 页码
$("#pages").on("click", "span", function (event) {
    const currentPage = parseInt($(event.target).text());
    renderBody(data, currentPage);
});
// 上一页
$("#pre").on("click", function () {
    const currentPage = parseInt($("#pages > .currentPage").text());
    if (currentPage === 1) {
        return;
    }
    else {
        renderBody(data, currentPage - 1);
    }
});
// 下一页
$("#next").on("click", function () {
    const currentPage = parseInt($("#pages > .currentPage").text());
    if (currentPage === $("#pages span").length) {
        return;
    }
    else {
        renderBody(data, currentPage + 1);
    }
});
let a = 1;
// 全选
$("#checkAll").on("click", function () {

    if (a % 2 === 1) {
        if ($("#information td input").prop("checked") === false) {
            for (let i = 0; i < data.length; ++i) {
                const item = data[i];
                item.isComplete = true;
            }
        }
        else {
            for (let i = 0; i < data.length; ++i) {
                const item = data[i];
                item.isComplete = true;
            }
        }
    }
    else {
        if ($("#information td input").prop("checked") === false) {
            for (let i = 0; i < data.length; ++i) {
                const item = data[i];
                item.isComplete = false;
            }
        }
        else {
            for (let i = 0; i < data.length; ++i) {
                const item = data[i];
                item.isComplete = false;
            }
        }
    }
    a = a + 1;
    renderBody(data);
});

// 删除
$("tbody").on("click", ".delete", function (event) {
    let dataId = parseInt($(event.target).attr("id"));
    for (let i = 0; i < data.length; ++i) {
        if (dataId === data[i].id) {
            $(".modal-body").append(`
            <span>姓名：${data[i].name}，</span>
            <span>id： ${data[i].id}</span>
        `);
        }
    }
    $("#btn-primary").on("click", function () {
        $('#myModal').modal('hide');
        for (let i = 0; i < data.length; ++i) {
            if (dataId === data[i].id) {
                data.splice(i, 1);
            }
        }
        renderBody(data, currentPage = 1);
    });
});


// 定薪
$("tbody").on("click", ".particulars", function (event) {
    window.location.href = "../薪资/定薪.html";
    const dataId = parseInt($(event.target).attr("id"));
    localStorage.setItem("id", dataId);

});

    
renderBody(data);
